package prop;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author Camilo
 * Classe para carregar as informações do prop.properties
 */
public class Prop {
  private String caminhoprop;
  private String caminhoxml,
          psqlIp,
          psqlPassword,
          sqlsIp,
          sqlsPassword;
  private Properties prop;

  public Prop() {
    this.setCaminhoprop("./prop/prop.properties");
  }
  
  public static Properties getProperties() throws IOException {
    Properties prop = new Properties();
    FileInputStream file = new FileInputStream(new Prop().getCaminhoprop());
    prop.load(file);
    return prop;
  }
  
  public Prop getProp() throws IOException{
    Properties p = new Properties();
    FileInputStream file = new FileInputStream(new Prop().getCaminhoprop());
    p.load(file);
    this.setCaminhoxml(p.getProperty("caminhoxml"));
    this.setSqlsIp(p.getProperty("sqls.ip"));
    this.setSqlsPassword(p.getProperty("sqls.password"));
    this.setPsqlIp(p.getProperty("psql.ip"));
    this.setPsqlPassword(p.getProperty("psql.password"));
    return this;
  }
  
  public void setProp() throws IOException {
    File file = new File(new Prop().getCaminhoprop());
    FileOutputStream fos = new FileOutputStream(file);
    
    prop = Prop.getProperties();
    
    prop.setProperty("psql.ip", this.getPsqlIp());
    prop.setProperty("psql.password", this.getPsqlPassword());
    prop.setProperty("sqls.ip", this.getSqlsIp());
    prop.setProperty("sqls.password", this.getSqlsPassword());
    prop.setProperty("caminhoxml", this.getCaminhoxml());
    
    prop.store(fos, "Gravando Configurações em prop.properties");    
    fos.close();
    
    this.getProp();
  }
  /*public static void main(String[] args) throws IOException {
    String ip = null, password = null;
    File file = new File("./properties/prop.properties");
    FileOutputStream fos = new FileOutputStream(file);
    Properties prop = getProperties();
    prop.setProperty("psql.ip", "192.168.0.2");
    prop.setProperty("psql.password", "123123");
    prop.setProperty("sqls.ip", "192.168.0.1");
    prop.setProperty("sqls.password", "456456");
    prop.store(fos, "Gravando Configurações em prop.properties");    
    fos.close();
    
    ip = prop.getProperty("psql.ip");
    password = prop.getProperty("psql.password");
    
    System.out.println(ip + " --- " + password 
            + "\n##...##...##...##...##\n" 
            + prop.getProperty("sqls.ip") + " --- "
            + prop.getProperty("sqls.password")); 
  }*/

  public String getCaminhoxml() {
    return caminhoxml;
  }

  public void setCaminhoxml(String caminhoxml) {
    this.caminhoxml = caminhoxml;
  }

  public String getPsqlIp() {
    return psqlIp;
  }

  public void setPsqlIp(String psqlIp) {
    this.psqlIp = psqlIp;
  }

  public String getPsqlPassword() {
    return psqlPassword;
  }

  public void setPsqlPassword(String psqlPassword) {
    this.psqlPassword = psqlPassword;
  }

  public String getSqlsIp() {
    return sqlsIp;
  }

  public void setSqlsIp(String sqlsIp) {
    this.sqlsIp = sqlsIp;
  }

  public String getSqlsPassword() {
    return sqlsPassword;
  }

  public void setSqlsPassword(String sqlsPassword) {
    this.sqlsPassword = sqlsPassword;
  }

  public String getCaminhoprop() {
    return caminhoprop;
  }

  public void setCaminhoprop(String caminhoprop) {
    this.caminhoprop = caminhoprop;
  }
}
